package com.sky.service.impl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * @description 分页查询
     * @return
     * @author yunfeng
     * @date 2023/5/5 09:50:36
     */
    @Override
    public PageResult findByPage(CategoryPageQueryDTO categoryPageQueryDTO) {
        //使用分页插件
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        Page<Category> page = categoryMapper.selectByPage(categoryPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * @description 新增分类
     * @param categoryDTO
     * @return
     * @author yunfeng
     * @date 2023/5/5 10:25:50
     */
    @Override
    public void add(CategoryDTO categoryDTO) {
        //组装数据
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        //设置默认状态为禁用状态
        category.setStatus(StatusConstant.DISABLE);
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.insert(category);
    }

    /**
     * @description 根据类型查询分类
     * @param type
     * @return
     * @author yunfeng
     * @date 2023/5/5 11:03:12
     */
    @Override
    public List<Category> findByType(Integer type) {
        List<Category> list = categoryMapper.selectByType(type);
        return list;
    }

    /**
     * @description 修改分类
     * @param categoryDTO
     * @return
     * @author yunfeng
     * @date 2023/5/5 11:23:20
     */
    @Override
    public void modify(CategoryDTO categoryDTO) {
        //搬运数据
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        category.setUpdateUser(BaseContext.getCurrentId());
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.update(category);
    }

    /**
     * @description 修改分类状态
     * @param status
     * @return
     * @author yunfeng
     * @date 2023/5/5 12:07:14
     */
    @Override
    public void modifyStatus(Integer status, Long id) {
        //组装数据
        Category category = new Category();
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
        category.setStatus(status);
        category.setId(id);
        categoryMapper.update(category);
    }

    /**
     * @description 删除分类
     * @param id
     * @return
     * @author yunfeng
     * @date 2023/5/5 12:17:15
     */
    @Override
    public void remove(Long id) {
        //判断分类下是否有套餐或者菜品
        //根据分类id查询套餐和菜品,判断,有一个不为null,代表分类下有产品,分别抛出异常
        List<Dish> dishList = dishMapper.getByCategoryId(id);
        if (dishList != null && dishList.size()>0) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        List<Setmeal> setmealList = setmealMapper.getSetmeal(id);
        if (setmealList != null&&setmealList.size()>0) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        categoryMapper.delete(id);
    }
    /**
     * 按照类型查询分类
     * @param list
     * @return
     */
    @Override
    public List<Category> get(Integer list) {
        List<Category> category = categoryMapper.get(list);

        return category;
    }
}
